package com.app.utils;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Copyright (C) @2020 fgwang.660@gmail.com
 *
 * @author witt
 * @version 1.0
 * @className WhereCause
 * @date 2020-05-20 22:16
 * @description SQL查询筛选条件
 */

public class WhereCause {

	private String joiner;

	private String key;

	private String condition;

	private Object value;

	// 是不是数字
	private Boolean isNumber = false;

	// SQL 连接符
	public final static String AND = " AND ";
	public final static String OR = " OR ";
	public final static String IN = " IN ";
	public final static String NOT_IN = " NOT IN ";
	public final static String EQUAL = " = ";
	public final static String NOT_EQUAL = " != ";
	public final static String GREATER_THAN = " > ";
	public final static String LESS_THAN = " < ";
	public final static String GREATER_OR_EQUAL_TO = " >= ";
	public final static String LESS_THAN_OR_EQUAL_TO = " <= ";


	/**
	 * 从参数中获得完整的where条件表达式
	 *
	 * @param params
	 * @return
	 */
	public static String getFullWhereCause(List<WhereCause> params) {
		StringBuilder sb = new StringBuilder();
		if (UtilTools.isNotEmpty(params)) {
			sb.append(" WHERE ");
			WhereCause first = params.remove(0);
			if (UtilTools.isNotEmpty(first.getValue())) {
				sb.append(first.getBooleanCause());
			}
			params.forEach(item -> {
				if (UtilTools.isNotEmpty(item.getValue())) {
					sb.append(item.getCause());
				}
			});
		}
		return sb.toString();
	}

	/**
	 * 获取完整的SQL条件表达式
	 *
	 * @return
	 */
	public String getCause() {
		return new StringBuilder()
				.append(this.getJoiner())
				.append(this.getKey())
				.append(this.getCondition())
				.append(this.getValue()).toString();
	}

	/**
	 * 获取布尔条件式
	 *
	 * @return
	 */
	public String getBooleanCause() {
		return new StringBuilder()
				.append(this.getKey())
				.append(this.getCondition())
				.append(this.getValue()).toString();
	}

	public Object getValue() {
		Object result = this.value;
		if ((IN.equals(this.condition) || NOT_IN.equals(this.condition)) && value instanceof String) {
			if (this.isNumber) {
				result = " (" + value + ") ";
			} else {
				String[] values = ((String) value).split("[,]");
				result = " (" + Arrays.stream(values)
						.map(item -> " '" + item + "' ").
								collect(Collectors.joining(",")) + ") ";
			}
		}else{
			if (! this.isNumber) {
				result = "'" + this.value + "'";
			}
		}
		return result;
	}

	public String getJoiner() {
		return joiner;
	}

	public WhereCause setJoiner(String joiner) {
		this.joiner = joiner;
		return this;
	}

	public String getKey() {
		return key;
	}

	public WhereCause setKey(String key) {
		this.key = key;
		return this;
	}

	public String getCondition() {
		return condition;
	}

	public WhereCause setCondition(String condition) {
		this.condition = condition;
		return this;
	}

	public WhereCause setValue(Object value) {
		this.value = value;
		return this;
	}

	public Boolean getNumber() {
		return isNumber;
	}

	public WhereCause setNumber(Boolean number) {
		isNumber = number;
		return this;
	}
}
